/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.figures.widget;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Cursors;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Locator;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.handles.AbstractHandle;
import org.eclipse.gef.tools.ResizeTracker;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.fireflow2.designer.eclipse.figures.CommonFigureInterface;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

/**
 * 圆形的，用于改变尺寸的锚点
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class CircleResizeHandle extends AbstractHandle {
	private int cursorDirection = 0;
	
	protected static final int DEFAULT_HANDLE_SIZE = 8;
	{
		init();
	}
	public CircleResizeHandle() {
	}
	public CircleResizeHandle(GraphicalEditPart owner,int direction) {
		super(owner, null);
		this.setLocator(new MyLocator(owner,direction));
		setCursor(Cursors.getDirectionalCursor(direction, owner.getFigure()
				.isMirrored()));
		this.cursorDirection = direction;
	}
	public CircleResizeHandle(GraphicalEditPart owner,int direction,  Cursor c) {
		super(owner, null, c);
		this.setLocator(new MyLocator(owner,direction));
		this.cursorDirection = direction;
	}
	
	/**
	 * Returns the color for the outside of the handle.
	 * 
	 * @return the color for the border
	 */
	protected Color getBorderColor() {
		return  Util4Eclipse.colorStr2ColorObj("#0000FF");
//		return (isPrimary()) ? ColorConstants.white : ColorConstants.black;
	}

	/**
	 * Returns the color for the inside of the handle.
	 * //Util4Eclipse.colorStr2ColorObj("#32CD32") 
	 * @return the color of the handle
	 */
	protected Color getFillColor() {
		return ColorConstants.white;
//		return (isPrimary())?ColorConstants.lightGray  : ColorConstants.white;
	}
	protected boolean isPrimary() {
		return getOwner().getSelected() == EditPart.SELECTED_PRIMARY;
	}

	
	protected void init() {
		setPreferredSize(new Dimension(DEFAULT_HANDLE_SIZE, DEFAULT_HANDLE_SIZE));
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.handles.AbstractHandle#createDragTracker()
	 */
	@Override
	protected DragTracker createDragTracker() {
		return new ResizeTracker(getOwner(), cursorDirection);
	}
	public void paintFigure(Graphics g) {
		Rectangle r = getBounds();
		r.shrink(1, 1);
		try {
			g.setBackgroundColor(getFillColor());
			g.fillOval(r.x, r.y, r.width, r.height);
			g.setForegroundColor(getBorderColor());
			g.drawOval(r.x, r.y, r.width, r.height);
		} finally {
			// We don't really own rect 'r', so fix it.
			r.expand(1, 1);
		}
	}
	
	class MyLocator implements Locator{
		GraphicalEditPart owner = null;
		int direction = PositionConstants.NORTH_EAST;
		public MyLocator(GraphicalEditPart owner,int direction){
			this.owner = owner;
			this.direction = direction;
		}
		/* (non-Javadoc)
		 * @see org.eclipse.draw2d.Locator#relocate(org.eclipse.draw2d.IFigure)
		 */
		public void relocate(IFigure target) {
			CommonFigureInterface figure = (CommonFigureInterface)owner.getFigure();
			Rectangle rectangle = figure.getSelectionHandleBounds();
			target.translateToRelative(rectangle);
			Rectangle handleBounds = new Rectangle();
			handleBounds.width = DEFAULT_HANDLE_SIZE;
			handleBounds.height = DEFAULT_HANDLE_SIZE;
			
			switch (direction) {
			case PositionConstants.NORTH_WEST:
				handleBounds.x = rectangle.x;
				handleBounds.y = rectangle.y;
				break;
			case PositionConstants.NORTH_EAST:
				handleBounds.x = rectangle.x+(rectangle.width-DEFAULT_HANDLE_SIZE);
				handleBounds.y = rectangle.y;
				break;
			case PositionConstants.SOUTH_WEST:
				handleBounds.x = rectangle.x;
				handleBounds.y = rectangle.y+(rectangle.height-DEFAULT_HANDLE_SIZE);
				break;
			case PositionConstants.SOUTH_EAST:
				handleBounds.x = rectangle.x+(rectangle.width-DEFAULT_HANDLE_SIZE);
				handleBounds.y = rectangle.y+(rectangle.height-DEFAULT_HANDLE_SIZE);
				break;

			case PositionConstants.EAST:
				handleBounds.x = rectangle.x+(rectangle.width-DEFAULT_HANDLE_SIZE);
				handleBounds.y = rectangle.y+(rectangle.height-DEFAULT_HANDLE_SIZE)/2;
				break;
			case PositionConstants.SOUTH:
				handleBounds.x = rectangle.x+(rectangle.width-DEFAULT_HANDLE_SIZE)/2;
				handleBounds.y = rectangle.y+(rectangle.height-DEFAULT_HANDLE_SIZE);
				break;
			case PositionConstants.WEST:
				handleBounds.x = rectangle.x;
				handleBounds.y = rectangle.y+(rectangle.height-DEFAULT_HANDLE_SIZE)/2;
				break;
				
			case PositionConstants.NORTH:
				handleBounds.x = rectangle.x+(rectangle.width-DEFAULT_HANDLE_SIZE)/2;
				handleBounds.y = rectangle.y;
				break;
			}

			target.setBounds(handleBounds);
			
		}
	}
}
